<?php
use PFT\Db;
if ($_SESSION['sid']!=1) {
    exit('{"status":"fail","msg":"权限验证失败"}');
}
$conf = include '../../common/db.conf.php';
include '../../common/Db.class.php';
include '../../../common/func.inc.php';

$action = safetxt($_REQUEST['action']);
//获取服务器数据
Db::Conf($conf['remote_1']);
$con=Db::Connect();
$con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
//保存表单
if($action=='save') {
    $fid               = intval($_POST['accountid']);
    $adstatus          = safetxt($_POST['adstatus']);
    $forbiden          = safetxt($_POST['forbiden']);
    $max_debt          = is_numeric(safetxt($_POST['max_debt']))? safetxt($_POST['max_debt']):"";
    $delay_days        = is_numeric(safetxt($_POST['delay_days']))? safetxt($_POST['delay_days']):"" ;
    $proof_price       = is_numeric(safetxt($_POST['proof_price']))? safetxt($_POST['proof_price']):"";
    $pricing_package   = is_numeric(safetxt($_POST['pricing_package']))?safetxt($_POST['pricing_package']):"";
    $package_order_num = is_numeric(safetxt($_POST['package_order_num']))?safetxt($_POST['package_order_num']):"";
    $package_startdate = safetxt($_POST['package_startdate']);
    $package_enddate   = safetxt($_POST['package_enddate']);
    //判断记录是否已存在
    $sql="select fid from pft_account_set where fid=? LIMIT 1";
    $exist_id = Db::fetch($sql,array($fid));

    $param = array(
        
        $adstatus,
        $forbiden,
        $max_debt,
        $delay_days,
        $proof_price,
        $pricing_package,
        $package_order_num,
        $package_startdate,
        $package_enddate
    );
    if($exist_id) {
		array_push($param,$fid);
        $sql= <<<SQL
UPDATE pft_account_set SET adstatus=?,forbiden_reason=?,
max_debt=?,arrears_days=?,proof_price=?,package_order_num=?,princing_package=?,
package_startdate=?,package_enddate=? where fid=? LIMIT 1
SQL;
    }
    else {
        array_unshift($param, $fid);
        //数据表中无此id时，直接插入
        $sql=<<<SQL
INSERT INTO pft_account_set(fid,adstatus,forbiden_reason,max_debt,
arrears_days,proof_price,package_order_num,princing_package,package_startdate,
package_enddate)values(?,?,?,?,?,?,?,?,?,?)
SQL;
    }

    $ret = Db::execute($sql, $param);
    if($ret===true) {
        echo '{"outcome":"ok","msg":"保存成功！"}';
    } else {
        echo '{"outcome":"fail","msg":"保存失败,错误信息："}';
    }
}
elseif ($action=='get_info') {
    $sql = "SELECT * FROM pft_account_set WHERE fid=? LIMIT 1";
    $row = Db::fetch($sql, array(intval($_REQUEST['id'])));
    if ($row) {
		$row=array(
		'adstatus'=>$row['adstatus'],
		'forbiden'=>$row['forbiden_reason'],
		'max_debt'=>$row['max_debt'],
		'delay_days'=>$row['arrears_days'],
		'proof_price'=>$row['proof_price'],
		'pricing_package'=>$row['princing_package'],
		'package_order_num'=>$row['package_order_num'],
		'package_startdate'=>$row['package_startdate'],
		'package_enddate'=>$row['package_enddate']
		
		);
        echo json_encode($row);
    } else {
        echo '[]';
    }
}
?>